<?php

class Sys_UserController extends Zend_Controller_Action
{

	private $request;
	private $common;
	private $db;
    public function init()
    {
		$this->request = $this->getRequest();

		include_once "../library/inc/Zyt_Common.php"; 
		$this->common = Zyt_Common::getInstance();
		
		$this->common->checklogin(true, true);

		include_once "../library/inc/Db_Config.php";
		$this->db = Db_Config::getInstance()->Base();
        /* Initialize action controller here */
    }

	public function passmodifyAction(){

		if($this->request->isXmlHttpRequest() && $this->request->isPost()) {
			$LoginPass = $this->request->getParam("LoginPass");
			$NewLoginPass = $this->request->getParam("NewLoginPass");
			$NewLoginPass2 = $this->request->getParam("NewLoginPass2");
			$userid = $this->common->getUserid();
			if(trim($NewLoginPass) != trim($NewLoginPass2)) {
				$this->common->PutData('{result:"",error:"两次输入密码不一致，请重新输入!"}');
			}else{

				$pwd = $this->db->fetchOne("SELECT password FROM user WHERE userid = :userid", array(
					"userid" => $userid
				));
				if(md5($LoginPass) != $pwd) {
					$this->common->PutData('{result:"",error:"老密码输入有误!"}');
				}
				else if($this->db->update("user", array(
					"password" => md5($NewLoginPass)
				), $this->db->quoteInto('userid = ?', $userid)))
				{
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"密码修改失败!"}');
				}
			}
		}else{
			$this->common->PutData('{result:"",error:"非法的请求，请重试或与管理员联系..."}');
		}
		exit;
	}

	public function personinfoAction()
	{
		$userid = $this->common->getUserid();

		$result = $this->getDetail($userid);

		$this->common->PutData("{record:[{UserID:" . $result["userid"] . ",DepartmentID:'" . $result["departname"] . "', UserName:'" . $result["username"] . "', sex:'" . $result["sex"] . "',Tel:'" . $result["tel"] . "',MobileTel:'" . $result["mobile"] . "',MailAddr:'" . $result["email"] . "',Remark:'" . $result["remark"] . "', QQ:'" . $result["qq"] . "'}]}");
		exit;
	}
	
	public function personsaveAction()
	{
		if($this->request->isXmlHttpRequest() && $this->request->isPost()) {
			$departmentid = $this->request->getParam("DepartmentID");
			$sex = $this->request->getParam("sex");
			$tel = $this->request->getParam("Tel");
			$mobile = $this->request->getParam("MobileTel");
			$qq = $this->request->getParam("QQ");
			$email = $this->request->getParam("MailAddr");
			$remark = $this->request->getParam("Remark");
			$userid = $this->common->getUserid();
			if($this->db->update("user", array(
					"departmentid" => $departmentid,
					"sex"		   => $sex,
					"tel"		   => $tel,
					"mobile"       => $mobile,
					"qq"           => $qq,
					"email"        => $email,
					"remark"	   => $remark
				), $this->db->quoteInto('userid = ?', $userid))) {
				$this->common->PutData('{result:""}');
			}
			else
			{
				$this->common->PutData('{result:"",error:"信息修改失败!"}');
			}
		
		}else{
			$this->common->PutData('{result:"",error:"非法的请求，请重试或与管理员联系..."}');
		}
		exit;
	}

	function getDetail($userid){
		$resul = $this->db->fetchRow("SELECT u.*, d.departname FROM user u INNER JOIN department d ON u.departmentid = d.departmentid WHERE u.userid = :userid", array(
			"userid" => $userid
		));
		
		return $resul;
	}

	public function getlistAction()
	{
		$username = $this->request->getParam("username");
		$start = $this->request->getParam("start");
		$limit = $this->request->getParam("limit");
		$where = "WHERE u.status & 1 = 0 ";
		if(!empty($username)) {
			$where .= " AND u.username like '%$username%'";
		}
		$_iAmount = $this->db->fetchOne("SELECT count(userid) FROM user u $where");
		$result = $this->db->fetchAll("SELECT u.userid, u.departmentid, d.departname, u.username, u.sex, u.tel, u.mobile, u.email, u.remark, u.last_login, u.last_ip, u.login_times, u.isOnline, u.optionset, u.status, u.createon FROM user u INNER JOIN department d on u.departmentid = d.departmentid $where limit $start, $limit");
		$sMsg = "";
		$len = count($result);
		for($i=0; $i < $len; $i++) {
			$comma = ",";
			if($i == ($len -1)) 
			{
				$comma = "";
			}
			$sMsg .= "{UserID:" . $result[$i]["userid"] . ",DepartmentID:" . $result[$i]["departmentid"] . ",DepartmentName:'" . $result[$i]["departname"] . "',LoginName:'" . $result[$i]["username"] . "', sex:'" . $result[$i]["sex"] . "',Tel:'" . $result[$i]["tel"] . "',MobileTel:'" . $result[$i]['mobile'] . "',MailAddr:'" . $result[$i]["email"] . "',Remark:'" . $result[$i]["remark"] . "',LastLoginTime:'" . $result[$i]["last_login"] . "',LastLoginIP:'" . $result[$i]["last_ip"] . "',LoginAmount:'" . $result[$i]["login_times"] . "', IsOnline:'" . $result[$i]["isOnline"] . "', OptionSet:" . $result[$i]["optionset"] . " ,CreateOn:'" . $result[$i]["createon"] . "',Status:'" . $result[$i]["status"] . "',LockStatus:'" . $result[$i]["status"] . "'}" . $comma;
		}

		$acl = array("aRead" => 1, "aNew" => 1, "aEdit" => 1, "aLock" => 1, "aDel" => 1, "aList" => 1);
		$_sExtJSD = "Acl:[{aRead:" . $acl["aRead"] . ", aNew:" . $acl["aNew"] . ",aEdit:" . $acl["aEdit"] . ",aDel:" . $acl["aDel"] .
							  ",aList:" . $acl["aList"] . "}]";
		$this->common->PutData('{totalProperty:' . $_iAmount . ',records:[' . $sMsg . '],' . $_sExtJSD . '}');
		exit;
	}

	public function saveAction()
	{
			$Uuserid = $this->request->getParam("UserID");
			$Uuserid = empty($Uuserid) ? 0 : $Uuserid;
			$departmentid = $this->request->getParam("DepartmentID");
			$username = $this->request->getParam("LoginName");
			$password = $this->request->getParam("LoginPass");
			$sex = $this->request->getParam("sex");
			$tel = $this->request->getParam("Tel");
			$mobile = $this->request->getParam("MobileTel");
			$qq = $this->request->getParam("QQ");
			$email = $this->request->getParam("MailAddr");
			$remark = $this->request->getParam("Remark");
			$userid = $this->common->getUserid();
			//$front = Zend_Controller_Front::getInstance();
			//$front->setParam('noErrorHandler', true)->throwExceptions(false)->returnResponse(true);
			//$response = $front->dispatch();
			
		if($Uuserid > 0) {
			if($this->db->update("user", array(
					"departmentid" => $departmentid,
					"username"    => $username,
					//"password"    => md5($password),
					"sex"		  => $sex,
					"tel"		=> $tel,
					"mobile"    => $mobile,
					"qq"		=> $qq,
					"email"		=> $email,
					"remark"	=> $remark
				), $this->db->quoteInto('userid = ?', $Uuserid)))
				{
					$this->common->PutData('{result:""}');
				}
				else
				{
					$this->common->PutData('{error:"保存失败!"}');
				}
		}
		else
		{
			if($this->db->insert("user", array(
				"departmentid" => $departmentid,
				"username"    => $username,
				"password"    => md5($password),
				"sex"		  => $sex,
				"tel"		=> $tel,
				"mobile"    => $mobile,
				"qq"		=> $qq,
				"email"		=> $email,
				"remark"	=> $remark,
				"isOnline"  => 0,
				"optionset" => 0,
				"status"    => 0,
				"createon"  => $this->common->getGmtime(),
				"last_login" => 0,
				"last_ip"   => "",
				"login_times" => 0
			))) {
				
				$this->common->PutData('{result:""}');
			}
			else
			{
				$this->common->PutData('{error:"保存失败!"}');
			}
		}

		exit;
	}

	public function spwdAction()
	{
		$password  = $this->request->getParam("LoginPass");
		$password2 = $this->request->getParam("NewLoginPass");
		$Uuserid   = $this->request->getParam("UserID");
		if($password != $password2) 
		{
			$this->common->PutData('{error:"两次输入密码不一致，请重新输入!"}');
		}
		else if($this->db->update("user", array(
					"password" => md5($password)
				), $this->db->quoteInto('userid = ?', $Uuserid))) 
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"更改失败!"}');
		}
		exit;
	}

	public function delAction()
	{
		$uids = $this->request->getParam("aid");
		
		if($this->db->update("user", array(
					"status" => 1
				), $this->db->quoteInto('userid in (?)', $uids))) 
		{
			
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}

	public function lockAction()
	{
		$uids = $this->request->getParam("aid");
		
		$stmt = $this->db->query("UPDATE user SET status = 2 - status & 2 WHERE status in (0,2) AND " . $this->db->quoteInto('userid in (?)', $uids));
		if($stmt->rowCount())
		{
			$this->common->PutData('{result:""}');
		}
		else
		{
			$this->common->PutData('{error:"操作失败!"}');
		}
		exit;
	}
}